package com.medata.utils;

import com.medata.dataObject.Templatefields;
import com.medata.service.TemplatefieldsService;

import java.util.List;

/**
 * @author ：zb
 * @date ：Created in 2019/5/30 0030 10:26
 * @description：
 */

public class SolrXmlUtil {
    public static String returnXml(TemplatefieldsService templatefieldsService, String dbid) {
        List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(Integer.valueOf(dbid));
        String xml = "<returnfield>";
        for (Templatefields templatefields : templatefieldsList) {
            String sxml = " <fieldname desc=\"" + templatefields.getTpFieldDesc() + "\">" + templatefields.getTpFieldName() + "</fieldname>";
            xml = xml + sxml;
        }
        xml = xml + "<fieldname desc=\"更新时间\">CreateDate</fieldname>" + "<fieldname desc=\"是否有文件\">hasobject</fieldname>" + "</returnfield>";
        return xml;
    }

    //获取词典的返回字段
    public static String returnXml2(TemplatefieldsService templatefieldsService, String dbid, String returnWords) {
        List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(Integer.valueOf(dbid));
        String xml = "<returnfield>";
        for (Templatefields templatefields : templatefieldsList) {
            if (returnWords.contains(templatefields.getTpFieldName())) {
                String sxml = " <fieldname desc=\"" + templatefields.getTpFieldDesc() + "\">" + templatefields.getTpFieldName() + "</fieldname>";
                xml = xml + sxml;
            }
        }
        xml = xml + "</returnfield>";
        return xml;
    }


    public static String headXml(String dbid) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<parameter><action>ADVSEARCH</action><isred>0</isred>";
        xml = xml + "<dbid>" + dbid + "</dbid>" + "<changeparameter>" + dbid + "</changeparameter>" + "<pagedown>0</pagedown>";
        return xml;
    }

    public static String searchXml(TemplatefieldsService templatefieldsService, Integer dbid, String word, String str, String curpage) {
        List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(dbid);
        Templatefields templatefields = null;
        if (word != null) {
            for (Templatefields templatefields1 : templatefieldsList) {
                if (templatefields1.getTpFieldDesc().equalsIgnoreCase(word)) {
                    templatefields = templatefields1;
                }
            }
        }
        String xml = "<searchfield><advfield><field name=\"";
        if (str == null || str.isEmpty()) {
            str = String.valueOf(dbid);
            xml = xml + "DBID" + "\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\"" + str + "\">" + str + "</field>" + "</advfield><filterfield/><ranfield/></searchfield><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + "<curpage>" + curpage + "</curpage><pagenum>10</pagenum></parameter>";
        } else {
            xml = xml + templatefields.getTpFieldName() + "\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\"" + str + "\">" + str + "</field>" + "</advfield><filterfield/><ranfield/></searchfield><sortfield><fieldname orderby=\"1\" fieldtype=\"string\">CreateDate</fieldname></sortfield>";
            xml = xml + "<curpage>" + curpage + "</curpage><pagenum>10</pagenum></parameter>";
        }
        return xml;
    }

    public static String searchXml2(TemplatefieldsService templatefieldsService, Integer dbid, String searchKey, String str, String curpage,String pagesize) {
        List<Templatefields> templatefieldsList = templatefieldsService.getByDbid(dbid);
        Templatefields templatefields = null;
        for (Templatefields templatefields1 : templatefieldsList) {
            if (templatefields1.getTpFieldName().equalsIgnoreCase(searchKey)) {
                templatefields = templatefields1;
            }
        }
        String xml = "<searchfield><advfield><field name=\"";
        xml = xml + templatefields.getTpFieldName() + "\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\"" + str + "\">" + str + "</field>" + "</advfield><filterfield/><ranfield/></searchfield><sortfield><fieldname orderby=\"0\" fieldtype=\"string\">id</fieldname></sortfield>";
        xml = xml + "<curpage>" + curpage + "</curpage><pagenum>"+pagesize+"</pagenum></parameter>";
        return xml;
    }


}
